package com.ghck.server.service.impl;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ghck.server.dao.FriendTreatmentMapper;
import com.ghck.server.dao.FriendTreatmentPicMapper;
import com.ghck.server.model.FriendTreatment;
import com.ghck.server.model.FriendTreatmentPic;
import com.ghck.server.model.query.FriendTreatmentQuery;
import com.ghck.server.model.request.FriendTreatmentReq;
import com.ghck.server.model.response.FriendTreatmentRes;
import com.ghck.server.service.FriendTreatmentService;
import com.ghck.server.utils.FileStoreUtil;

/**
 * 亲友就诊记录  <br>
 * t_friend_treatment <br>
 * v1.0 2016-05-01
 */
 @Service
 @Transactional
 public class FriendTreatmentServiceImpl implements FriendTreatmentService {
	
	private static final Logger logger = LoggerFactory.getLogger(FriendTreatmentServiceImpl.class);
	
	@Autowired
	private FriendTreatmentMapper friendTreatmentMapper;
	
	@Autowired
	private FriendTreatmentPicMapper friendTreatmentPicMapper;
	/**
 	*根据ID删除 亲友就诊记录
 	*/
 	@Override
 	public Integer deleteFriendTreatmentById(Integer id){
 		return friendTreatmentMapper.deleteById(id);
 	}
 	
 	/**
 	*新增 亲友就诊记录
 	*/
 	@Override
 	@Transactional
 	public Integer batchInsertFriendTreatment(List<? extends FriendTreatment> friendTreatments){
		Integer count = friendTreatmentMapper.batchInsert(friendTreatments);
		return count;
 	}
 	/**
 	*新增 亲友就诊记录
 	*/
 	@Override
 	@Transactional
 	public Integer insertFriendTreatment(FriendTreatment domain){
 		Integer treatmentId = null;
 		FriendTreatmentReq treatment = null;
 		if(domain instanceof FriendTreatmentReq){
 			treatment = (FriendTreatmentReq) domain;
 			treatment.dealWith();
 			
 			friendTreatmentMapper.insert(treatment);
 			
 			treatmentId = treatment.getId();
 			Integer friendId = treatment.getFriendId();
 			List<FriendTreatmentPic> pics = treatment.getTreatmentPics();
 			
 			if (CollectionUtils.isNotEmpty(pics)) {
				for (FriendTreatmentPic treatmentPic : pics) {
					treatmentPic.setParentId(treatmentId);
					treatmentPic.setFriendId(friendId);
					treatmentPic.setInUsed(1);
				}
				friendTreatmentPicMapper.batchInsert(pics);
			}
 		}
		return treatmentId;
 	}

 	/**
 	*更新 亲友就诊记录
 	*/
 	@Override
 	@Transactional
 	public Integer updateFriendTreatment(FriendTreatment domain){
		friendTreatmentMapper.update(domain);
 		Integer treatmentId = domain.getId();
 		FriendTreatmentReq treatment = null;
 		if(domain instanceof FriendTreatmentReq){
 			treatment = (FriendTreatmentReq) domain;
 			treatment.dealWith();
 			Integer friendId = treatment.getFriendId();
 			friendTreatmentMapper.update(treatment);
 			if (ArrayUtils.isNotEmpty(treatment.getDeletePicIds())) {
 				List<Integer> list = Arrays.asList(treatment.getDeletePicIds());
 				friendTreatmentPicMapper.deleteByIds(list);
			}
 			List<FriendTreatmentPic> pics = treatment.getTreatmentPics();
 			if (CollectionUtils.isNotEmpty(pics)) {
				for (FriendTreatmentPic treatmentPic : pics) {
					treatmentPic.setParentId(treatmentId);
					treatmentPic.setFriendId(friendId);
					treatmentPic.setInUsed(1);
				}
				friendTreatmentPicMapper.batchInsert(pics);
			}
 			
 		}
		return treatmentId;
 	}
 	
 	/**
 	*根据ID查询 亲友就诊记录
 	*/
 	public FriendTreatment findFriendTreatmentById(Integer id){
 		FriendTreatment result = friendTreatmentMapper.findById(id);
 		if(result!=null){
 			result.dealWith();
 		}
 		return result;
 	}

	/**
 	*通过query 查询 亲友就诊记录
 	*/
	public List<FriendTreatmentRes> findFriendTreatmentByQuery(FriendTreatmentQuery query){
		query.dealWith();
		List<FriendTreatmentRes> result = friendTreatmentMapper.findByQuery(query);
		if(CollectionUtils.isNotEmpty(result)) {
			List<Integer> treatmentIds = new ArrayList<Integer>();
			Collections.sort(result);
			Map<Integer, FriendTreatmentRes> map = new HashMap<Integer, FriendTreatmentRes>();	
			for (FriendTreatmentRes treatment : result) {
				treatmentIds.add(treatment.getId());
				map.put(treatment.getId(), treatment);
			}
			if (CollectionUtils.isNotEmpty(treatmentIds)) {
				List<FriendTreatmentPic> pics =  friendTreatmentPicMapper.findByParentIds(treatmentIds);
				if (CollectionUtils.isNotEmpty(pics)) {
					for (FriendTreatmentPic treatmentPic : pics) {
						if(map.containsKey(treatmentPic.getParentId())){
							FileStoreUtil.dealWithTreatmentPic(treatmentPic);
							map.get(treatmentPic.getParentId()).addTreatmentPics(treatmentPic);
						}
					}
				}
			}
		}
		return result;
	}	
	
	/**
 	*通过query count总条数
 	*/
 	public Integer countFriendTreatmentByQuery(FriendTreatmentQuery query){
 		query.dealWith();
 		Integer result = friendTreatmentMapper.countByQuery(query);
 		return result;
 	}
 	
 	/**
 	* 通过ids查询
 	*/
 	public List<FriendTreatment> findFriendTreatmentByIds(List<Integer> ids){
 		List<FriendTreatment> result = friendTreatmentMapper.findByIds(ids);
		if(CollectionUtils.isNotEmpty(result)) {
			for (FriendTreatment friendTreatment : result) {
				friendTreatment.dealWith();
			}
		}
		return result;
 	}
 	
}	